//
//  EmptyView.swift
//  ASW
//
//  Created by zhuxuhong on 2020/11/26.
//

import BWUIKit
import BWExtensionKit
import Foundation
import UIKit

protocol EmptyReusableView {
    func loadEmptyView() -> EmptyView
}

extension Bundle {
    static func podResourceBundle(_ podName: String) -> Bundle? {
        guard var aURL = Bundle.main.url(forResource: "Frameworks", withExtension: nil) else {
            return nil
        }
        aURL.appendPathComponent(podName)
        aURL = aURL.appendingPathExtension("framework")
        
        guard
            let frameworkBundle = Bundle(url: aURL),
            let aURL = frameworkBundle.url(forResource: podName, withExtension: "bundle"),
            let resourceBundle = Bundle(url: aURL)
        else {
            return nil
        }
        
        return resourceBundle
    }
}

extension EmptyReusableView {
    func loadEmptyView() -> EmptyView {
        guard
            let bundle = Bundle.podResourceBundle("BWEmptyKit"),
            let view = bundle.loadNibNamed("\(EmptyView.self)", owner: nil, options: nil)?.first as? EmptyView
        else {
            return EmptyView()
        }
        return view
    }
}

public class EmptyView: UIView {
    @IBOutlet public weak var imageView: UIImageView?
    @IBOutlet public weak var label: UILabel?
    @IBOutlet public weak var button: UIButton?
    @IBOutlet public weak var stackView: UIStackView!
    
    static let defaultStyling: Empty.StylingHandler = {
        $0.imageView?.contentMode = .scaleAspectFit
        $0.imageView?.backgroundColor = .clear
        $0.imageView?.layer.cornerRadius = 0
        $0.imageView?.layer.borderWidth = 0
        
        $0.label?.font = UIFont.system14
        $0.label?.textColor = .lightGray
        $0.label?.layer.cornerRadius = 0
        $0.label?.layer.borderWidth = 0
        $0.label?.backgroundColor = .clear
        
        $0.button?.titleLabel?.font = UIFont.system14
        $0.button?.tintColor = .systemBlue
        $0.button?.layer.cornerRadius = 0
        $0.button?.layer.borderWidth = 0
        $0.button?.backgroundColor = .clear
        
        $0.stackView.spacing = 20
    }
    
    func updateData(_ data: EmptyData) {
        let styling = data.stylingHandler ?? Self.defaultStyling
        styling(self)
        
        imageView?.image = data.image
        label?.text = data.tips
        button?.setTitle(data.action?.title, for: .normal)
        button?.setImage(data.action?.icon, for: .normal)
        
        if let ac = data.action, let handler = ac.handler {
            button?.bw_tapped{ _ in handler() }
        }
        imageView?.isHidden = data.image == nil
        label?.isHidden = data.tips == nil
        button?.isHidden = data.action == nil
        
        alpha = 0
        transform = .init(scaleX: 0.85, y: 0.85)
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [.curveEaseInOut], animations: {
            self.alpha = 1
            self.transform = .init(scaleX: 1, y: 1)
        }, completion: nil)
    }
}
